﻿using Autofac;
using project.B12.application;
using project.B12.application.Ioc;
using project.B12.application.ViewModels.Setting;
using project.B12.IService;
using project.B12.Service.UserService;
using project.B12.Views;
using software.Common.MVVM;
using System;
using System.Windows;

namespace project.B12.ViewModels
{
    public class MainWindowViewModel  : ObservableObject
    {
        public IUserService userService { get; set; }
        public IWorkSequence workSequence { get; set; }
        public MainWindowViewModel(IUserService _userService, IWorkSequence _workSequence)
        {
            userService = _userService;
            workSequence = _workSequence;
        }
        #region Model
        private double width;//当前框架窗体宽度
        private double height;//当前框架窗体高度
        private FrameworkElement _currentView;//当前主窗体框架显示的视图
        private FrameworkElement loginView;//登陆视图
        private FrameworkElement mainView;//主界面视图
        public double Width
        {
            get { return width; }
            set { width = value; this.RaisePropertyChanged(); }
        }
        public double Height
        {
            get { return height; }
            set { height = value; this.RaisePropertyChanged(); }
        }
        public FrameworkElement CurrentView
        {
            get { return _currentView; }
            set { _currentView = value; this.RaisePropertyChanged(); }
        }
        public FrameworkElement LoginView
        {
            get { return loginView; }
            set { loginView = value; this.RaisePropertyChanged(); }
        }
        public FrameworkElement MainView
        {
            get { return mainView; }
            set { mainView = value; this.RaisePropertyChanged(); }
        }
        #endregion

        /// <summary>
        /// 创建登录界面
        /// </summary>
        public void CreateView()
        {
            LoginView = new LoginView();//登录视图
        }

        /// <summary>
        /// 第一次载入登录界面
        /// </summary>
        public void ChangeToLoginView()
        {
            try
            {
                ViewModelLocator.PublicResourceViewModel.windowState = WindowState.Normal;
                CurrentView = loginView;
                Width = 410;
                Height = 410;
            }
            catch (Exception)
            {
                throw new Exception();
            }
        }

        /// <summary>
        /// 切换主界面
        /// </summary>
        public void ChangeToMainView(bool newUser)
        {
            try
            {
                App.Current.Dispatcher.Invoke((Action)(() =>
                {
                    ViewModelLocator.PublicResourceViewModel.windowState = WindowState.Normal;
                    if (newUser)//新用户登陆，所有主界面相关的数据上下文重新初始化
                    {
                        workSequence.InitDataAsync();
                        ViewModelLocator.UserSettingViewModel.Init();//用户界面数据源初始化
                        userService.Init();
                        ViewModelLocator.AnalyseViewModel.Init();//分析界面初始化
                        mainView = new MainView();
                        CurrentView = mainView;
                    }
                    else//重复登录同一账号，仅切换界面
                    {
                        CurrentView = mainView;
                    }
                    Width = 1712;
                    Height = 963;
                }));
            }
            catch (Exception)
            {
                throw new Exception();
            }
        }
    }
}
